/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-03
 * Time: 9:52
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumNumbers(TreeNode root) {
        //需要重新构造一个函数体，传参为节点和前驱值
        return dfs(root,0);
    }
    public int dfs(TreeNode root , int preSum){
        preSum = 10 * preSum + root.val;
        //大局观：这一步深搜到叶子结点了，就返回叶子结点值
        if(root.left == null && root.right == null) return preSum;

        int ret = 0;
        //根节点的值 = 左子树的值 + 右子树的值
        if(root.left != null){
            ret += dfs(root.left,preSum);
        }
        if(root.right != null){
            ret += dfs(root.right,preSum);
        }
        return ret;
    }
}
public class Test2 {
}
